package integraltesk

import (
	"context"
	"strconv"
	"strings"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/setting/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/setting/rpc/settingclient"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetIntegralTaskListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetIntegralTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIntegralTaskListLogic {
	return &GetIntegralTaskListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetIntegralTaskListLogic) GetIntegralTaskList(req *types.IntegralTaskListReq) (*types.IntegralTaskListResp, error) {
	logx.Infof("GetIntegralTaskList : %+v ", req)
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorterNew(req.Sorter)
	filterBys := util.ParseFilterNew(req.Filter, req.Type)

	var ids string
	var name, slug *string
	if req.Search != nil && len(strings.TrimSpace(*req.Search)) > 0 { // 前端可以搜名称或者ID
		reqSearch := strings.TrimSpace(*req.Search)
		if _, err := strconv.ParseInt(reqSearch, 10, 64); err == nil {
			ids = reqSearch
		} else {
			name = pointy.GetPointer(reqSearch)
		}
	}
	if req.Name != nil && len(strings.TrimSpace(*req.Name)) > 0 {
		name = pointy.GetPointer(strings.TrimSpace(*req.Name))
	}
	if req.Slug != nil && len(strings.TrimSpace(*req.Slug)) > 0 {
		slug = pointy.GetPointer(strings.TrimSpace(*req.Slug))
	}

	if req.Id != nil && *req.Id > 0 {
		ids = strconv.FormatInt(int64(*req.Id), 10)
	}
	if req.Ids != nil && len(strings.TrimSpace(*req.Ids)) > 0 {
		ids = strings.TrimSpace(*req.Ids)
	}

	data, err := l.svcCtx.SettingRpc.BackendGetIntegralTaskList(l.ctx, &settingclient.BackendIntegralTaskListReq{
		Page:      page,
		PageSize:  pageSize,
		Ids:       pointy.GetPointer(ids),
		Name:      name,
		Slug:      slug,
		Status:    req.Status,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderBy:   orderBys,
		FilterBy:  filterBys,
		Type:      nil,
		UpdatedBy: nil,
	})
	if err != nil {
		return nil, err
	}

	resp := &types.IntegralTaskListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.IntegralTaskListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
			StatusStatisticMap: data.StatusStatisticMap,
		},
	}

	for _, v := range data.List {
		option := utils.GetAuditOptionByStatus(v.Status)
		resp.Data.List = append(resp.Data.List, &types.IntegralTaskInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			RuleName:      v.RuleName,
			RuleSlug:      v.RuleSlug,
			RuleType:      v.RuleType,
			PicId:         v.PicId,
			PicUrl:        v.PicUrl,
			Frequency:     v.Frequency,
			Points:        v.Points,
			EffectiveTime: v.EffectiveTime,
			Ip:            v.Ip,
			Description:   v.Description,
			Sort:          v.Sort,
			Status:        v.Status,
			DelStatus:     v.DelStatus,
		})
	}

	return resp, nil
}
